********************************************************************************
*** Voting for Populism in Europe Replication Files
*** Table A21: Regional Voting (Populist Parties) Unimputed Data
*** Table A22: Regional Voting (Centrist Parties) Unimputed Data
*** Table A23: Regional Voting, Low Wage Imports (Populist Parties) Unimputed Data
*** Table A24: Regional Voting, Low Wage Imports (Centrist Parties) Unimputed Data
***
*** Required data files: "CLEA_voteshare_turnout.dta"
***                     
***
***                      regional_data_impute.dta
***                     
***
*** 
*** Created by: Thomas Cunningham
*** Date: 5 January 2020
******************************************************************
***  Required installations
* ssc install weakivtest
* ssc install avar
* ssc install twostepweakiv 
* ssc install ivreg2
* ssc install moremata
* ssc install ranktest

clear all
version 16.1
cd ..

use "data/parties_votes/CLEA_voteshare_turnout.dta", clear

	* When there are two or more elections in a single year, keep only the first election
	drop if ctr_n == "Greece" & yr == 1989 & mn == 11
	drop if ctr_n == "Greece" & yr == 2012 & mn == 6
	drop if ctr_n == "Greece" & yr == 2015 & mn == 9
	drop if ctr_n == "Ireland" & yr == 1982 & mn == 11
	drop if ctr_n == "United Kingdom" & yr == 1974 & mn == 10
	
	* Rename and assign cow codes
	rename ctr_n cname 
	rename yr year
	ccode cname,from(CTY) to(COW) gen(ccode)
	ta cname if ccode == .
	order cname ccode year *

	* Subset to post-1990 and misc. voteshares that could not be assigned to a specific region
	drop if year <= 1990 
	drop if nuts2==""

	** Set cross-sectional and time-series vars
	encode nuts2, gen(nutsid)

	bysort nuts2 (year) : gen counter = _n  // To create lagged and first differences

	tsset nutsid counter 

	renvars nuts_turnout-nuts_other_parties,presub(nuts_ nuts2_) 
	renvars nuts2_right_pop-nuts2_other_parties,postfix(_vs)
	


replace nuts2="FRY5" if nuts2=="FRA5"
replace nuts2="FRY1" if nuts2=="FRA1"
replace nuts2="FRY2" if nuts2=="FRA2"
replace nuts2="FRY3" if nuts2=="FRA3"
replace nuts2="FRY4" if nuts2=="FRA4"
replace nuts2="FRF2" if nuts2=="FR21"
replace nuts2="FRE2" if nuts2=="FR22"
replace nuts2="FRD2" if nuts2=="FR23"
replace nuts2="FRB0" if nuts2=="FR24"
replace nuts2="FRD1" if nuts2=="FR25"
replace nuts2="FRC1" if nuts2=="FR26"
replace nuts2="FRE1" if nuts2=="FR3"
replace nuts2="FRF3" if nuts2=="FR41"
replace nuts2="FRF1" if nuts2=="FR42"
replace nuts2="FRC2" if nuts2=="FR43"
replace nuts2="FRG0" if nuts2=="FR51"
replace nuts2="FRH0" if nuts2=="FR52"
replace nuts2="FRI3" if nuts2=="FR53"
replace nuts2="FRI1" if nuts2=="FR61"
replace nuts2="FRJ2" if nuts2=="FR62"
replace nuts2="FRI2" if nuts2=="FR63"
replace nuts2="FRK2" if nuts2=="FR71"
replace nuts2="FRK1" if nuts2=="FR72"
replace nuts2="FRJ1" if nuts2=="FR81"
replace nuts2="FRL0" if nuts2=="FR82"
replace nuts2="FRM0" if nuts2=="FR83"


replace nuts2="FR1" if nuts2=="FR10"
replace nuts2="FRB" if nuts2=="FRB0"
replace nuts2="FRG" if nuts2=="FRG0"
replace nuts2="FRH" if nuts2=="FRH0"
replace nuts2="FRL" if nuts2=="FRL0"
replace nuts2="FRM" if nuts2=="FRM0"

	
	* Create lagged dependent variables and difference in vote share between elections
	qui{
		foreach v of varlist nuts2_*  {
			local lbl : var label `v'
			replace `v'  = 0 if `v'==.
			gen `v'_L1 = L1.`v'
			la var `v'_L1 `"Lag1: `lbl'"'
			gen `v'_diff = D.`v'
			la var `v'_diff `"$\Delta$ `lbl'"'
			}
		}

	su *

	tempfile voteshares
	save `voteshares'
 
	
use "data/imputed/regional_data_impute.dta", clear
encode nuts2,gen(nuts_enc)


xtset nuts_enc year

foreach var of varlist shock_china_ind shock_lowwage_ind rti_region off_region shock_fgn92 shock_robots_mfg shock_fdi_in_ind inst_china_ind inst_lowwage_ind{
	gen `var'_L1 = L.`var'
}

xtset, clear
drop nuts_enc

merge m:1 nuts2 year using `voteshares',
	keep if _merge ==3 // Drop non-election years

rename nuts2_main_left_vs mleft
rename nuts2_main_right_vs mright
rename nuts2_left_pop_vs lpop
rename nuts2_right_pop_vs rpop

encode nuts2,gen(nuts_encoded)
egen cwdyad = group(ccode year)
gen postcrisis = cond(year >=2008, 1, 0, .)



*------------- Relabel Key Covariates ----------------------------------- 
la var shock_china_ind_L1 "China Shock 1 Lag"
la var shock_lowwage_ind_L1 "Low-Wage Imp Shock 1 Lag"
la var rti_region_L1 "RTI Region 1 Lag"
la var shock_fgn92_L1 "Foreigner Shock 1 Lag"
la var shock_robots_mfg_L1 "Robot Shock 1 Lag"
la var shock_fdi_in_ind_L1 "FDI Shock 1 Lag"
la var postcrisis "Post-Crisis"	
la var inst_china_ind_L1 "US-China Instrument 1 Lag"
la var inst_lowwage_ind_L1 "US-Low-Wage Instrument 1 Lag"



* Dependent variables
local dvs "mleft mright lpop rpop"

* Control Variables
local controls0 "shock_fdi_in_ind_L1  shock_robots_mfg_L1"
local controls1 "shock_fdi_in_ind_L1  shock_robots_mfg_L1 rti_region_L1 shock_fgn92_L1"


*------------- REGRESSION LOOP ------------------------------------------ 



foreach v of  varlist `dvs' { // loop over dependent variables
	di "`v'"
	forval u=0/1{  //loop over controls
 *Non-Interaction			
	reg `v' shock_china_ind_L1 i.postcrisis  `controls`u''  i.cwdyad, robust cluster(nuts_encoded)
	est store cn_`v'_c`u'_1
	di "`v'"

sleep 5000
*Interact China Shock x Vote Share
	reg `v' c.shock_china_ind_L1##i.postcrisis   `controls`u''  i.cwdyad, robust cluster(nuts_encoded)
	est store pccn_`v'_c`u'_1
	di "`v'"
	sleep 5000
	
*Non-Interaction Low-Wage
	reg `v' shock_lowwage_ind_L1 i.postcrisis  `controls`u''  i.cwdyad, robust cluster(nuts_encoded)
	est store lw_`v'_c`u'_1
	di "`v'"
	sleep 5000

*Interaction Low-Wage Shock X Post Crisis
	reg `v' c.shock_lowwage_ind_L1##i.postcrisis   `controls`u''  i.cwdyad, robust cluster(nuts_encoded)
	est store pclw_`v'_c`u'_1
	di "`v'"
	sleep 5000
	
*2SLS China Estimation Model (Non-interaction)
	ivreg2 `v' i.postcrisis  `controls`u''  (shock_china_ind_L1=inst_china_ind_L1) i.cwdyad, partial(i.cwdyad) robust cluster(nuts_encoded) first, 
	est store cn_`v'_c`u'_1_inst
	di "`v'"
	sleep 5000
	
*2SLS Interact China Shock x Vote Share
	ivreg2 `v' `controls`u'' (c.shock_china_ind_L1##i.postcrisis=c.inst_china_ind_L1##i.postcrisis) i.cwdyad , partial(i.cwdyad) robust cluster(nuts_encoded) first, 
	est store pccn_`v'_c`u'_1_inst
	di "`v'"
	sleep 5000
	
*2SLS Lowwage Estimation (Non-interaction)
	ivreg2 `v' i.postcrisis  `controls`u''  (shock_lowwage_ind_L1=inst_lowwage_ind_L1) i.cwdyad , partial(i.cwdyad) robust cluster(nuts_encoded), 
	est store lw_`v'_c`u'_1_inst
	di "`v'"
	
*2SLS Lowwage Estimation (Interaction)
	ivreg2 `v' `controls`u'' (c.shock_lowwage_ind_L1##i.postcrisis=c.inst_lowwage_ind_L1##i.postcrisis) i.cwdyad, partial(i.cwdyad) robust cluster(nuts_encoded), 
	est store pclw_`v'_c`u'_1_inst
	di "`v'"
		}
	



* Instrument Table. All Models 
esttab cn_`v'_c0_1  cn_`v'_c0_1_inst cn_`v'_c1_1 cn_`v'_c1_1_inst pccn_`v'_c0_1 pccn_`v'_c0_1_inst pccn_`v'_c1_1  pccn_`v'_c1_1_inst using "output/appendix/unimputed_regional_voting_`v'.tex", replace /*
*/  cells(b(star fmt(%9.3f)) se(par)) stats(N, fmt(%9.0g %9.3f)) unstack starlevels(* .1 ** .05 *** .01)/*
	*/ label legend drop(*.cwdyad) nobaselevels  order(shock_china_ind_L1 1.postcrisis shock_fdi_in_ind_L1  shock_robots_mfg_L1 rti_region_L1 shock_fgn92_L1) nodepvars nomtitles  ///
	title("Regional Voting (1990-2018) \\ (Instrumental Variable, All original Models)} \begin{threeparttable") nonumber  nonotes compress ///
postfoot("\hline\hline \end{tabular} \begin{tablenotes} \footnotesize \item Notes: * p$<$.1, ** p$<$.05, *** p$<$.01. OLS estimates with country-year (i.e., election) fixed effects and robust standard errors clustered over 160 European regions (NUTS-1/2) in parentheses. The dependent variable fis vote share as a percentage by party family. Globalization shocks are three-year differences in total imports, inward FDI flows in manufacturing, and the stock of robots in manufacturing, all weighted by labor shares in industry, regional-level exposure to routinization and off-shoring, and the portion of migrants over the region's total population. \end{tablenotes} \end{threeparttable} \end{table}") 

* Instrument Table. Low Wage All Original Models 
esttab lw_`v'_c0_1  lw_`v'_c0_1_inst lw_`v'_c1_1 lw_`v'_c1_1_inst pclw_`v'_c0_1 pclw_`v'_c0_1_inst pclw_`v'_c1_1  pclw_`v'_c1_1_inst using "output/appendix/unimputed_regional_voting_lowwage_`v'.tex", replace /*
*/  cells(b(star fmt(%9.3f)) se(par)) stats(N, fmt(%9.0g %9.3f)) unstack starlevels(* .1 ** .05 *** .01)/*
	*/ label legend drop(*.cwdyad) nobaselevels  order(shock_lowwage_ind_L1 1.postcrisis shock_fdi_in_ind_L1  shock_robots_mfg_L1 rti_region_L1  shock_fgn92_L1) nodepvars nomtitles  ///
	title("Regional Voting (1990-2018) \\ (Instrumental Variable, All original Models)} \begin{threeparttable") nonumber  nonotes compress ///
postfoot("\hline\hline \end{tabular} \begin{tablenotes} \footnotesize \item Notes: * p$<$.1, ** p$<$.05, *** p$<$.01. OLS estimates with country-year (i.e., election) fixed effects and robust standard errors clustered over 160 European regions (NUTS-1/2) in parentheses. The dependent variable fis vote share as a percentage by party family. Globalization shocks are three-year differences in total imports, inward FDI flows in manufacturing, and the stock of robots in manufacturing, all weighted by labor shares in industry, regional-level exposure to routinization and off-shoring, and the portion of migrants over the region's total population. \end{tablenotes} \end{threeparttable} \end{table}")



estimates clear
}

